//package com.yupi.springbootinit.messagecenter;
//
//import com.yupi.springbootinit.config.RabbitMqConfig;
//import com.yupi.springbootinit.mq.EventEnvelope;
//import jakarta.annotation.Resource;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.amqp.rabbit.annotation.RabbitListener;
//import org.springframework.amqp.AmqpRejectAndDontRequeueException;
//import org.springframework.amqp.core.Message;
//import org.springframework.stereotype.Component;
//import java.util.Map;
//import java.nio.charset.StandardCharsets;
//
//@Component
//@Slf4j
//public class MessageCenterConsumers {
//
//    @Resource
//    private MessagePersistService persistService;
//
//    private void forceErrorIfRequested(EventEnvelope env) {
//        try {
//            Object payload = env.payload();
//            if (payload instanceof Map<?, ?> m) {
//                Object fe = m.get("forceError");
//                if (Boolean.TRUE.equals(fe) || "true".equalsIgnoreCase(String.valueOf(fe))) {
//                    log.warn("Force error for DLQ test. rk={}, type={}, id={}", env.routingKey(), env.type(), env.eventId());
//                    throw new AmqpRejectAndDontRequeueException("forceError=true");
//                }
//            }
//        } catch (AmqpRejectAndDontRequeueException e) {
//            throw e;
//        } catch (Exception ignore) {
//        }
//    }
//
//    // @RabbitListener(queues = RabbitMqConfig.QUEUE_MSG_IMMEDIATE)  // RabbitMQ 已暂停使用
//    public void onImmediate(EventEnvelope env) {
//        log.info("[IMMEDIATE] rk={}, type={}, id={}", env.routingKey(), env.type(), env.eventId());
//        forceErrorIfRequested(env);
//        persistService.persist("MessageCenterConsumer.immediate", env);
//    }
//
//    // @RabbitListener(queues = RabbitMqConfig.QUEUE_REMINDER_UNCLAIMED)  // RabbitMQ 已暂停使用
//    public void onUnclaimed(EventEnvelope env) {
//        log.info("[REMINDER-UNCLAIMED] rk={}, type={}, id={}", env.routingKey(), env.type(), env.eventId());
//        forceErrorIfRequested(env);
//        persistService.persist("MessageCenterConsumer.reminderUnclaimed", env);
//    }
//
//    // @RabbitListener(queues = RabbitMqConfig.QUEUE_REMINDER_OVERDUE)  // RabbitMQ 已暂停使用
//    public void onOverdue(EventEnvelope env) {
//        log.info("[REMINDER-OVERDUE] rk={}, type={}, id={}", env.routingKey(), env.type(), env.eventId());
//        forceErrorIfRequested(env);
//        persistService.persist("MessageCenterConsumer.reminderOverdue", env);
//    }
//
//    // @RabbitListener(queues = RabbitMqConfig.QUEUE_DLQ)  // RabbitMQ 已暂停使用
//    public void onDlq(Message message) {
//        String body = new String(message.getBody(), StandardCharsets.UTF_8);
//        log.error("[DLQ] rk={}, headers={}, body={}",
//                message.getMessageProperties().getReceivedRoutingKey(),
//                message.getMessageProperties().getHeaders(),
//                body);
//    }
//}
